package red.sms.business;

import javax.swing.Icon;

import red.sms.db.config.ServiceConfig;
import red.sms.db.dao.SmsMoDAO;
import red.sms.db.dao.SmsMtDAO;
import red.sms.db.entity.SmsMO;
import red.sms.domain.GetSmsResult;
import red.sms.domain.SendSmsResult;
import red.sms.util.IConstant;

public class MainProcess {

	/**
	 * TODO: getting SMS in MO Table
	 */
	public static GetSmsResult getSMS(String commandCode , String maDichVu){
		GetSmsResult getSmsResult = new GetSmsResult();
		//Step 1: Check commandCode, maDichVu
		//true  ->> valid
		//false ->> invalid
		boolean checkCommandCode = ServiceConfig.checkCommandCode(commandCode);
		if(!checkCommandCode){
			getSmsResult.setErrorCode(IConstant.ERROR_CODE.INVALID_COMMAND_CODE);
			getSmsResult.setErrorMessage(IConstant.ERROR_MESSAGE.INVALID_COMMAND_CODE);
			getSmsResult.setContent("");
			return getSmsResult;
		}
		
		boolean checkMaDichVu = ServiceConfig.checkMaDichVu(maDichVu);
		if(!checkMaDichVu){
			getSmsResult.setErrorCode(IConstant.ERROR_CODE.INVALID_MA_DICH_VU);
			getSmsResult.setErrorMessage(IConstant.ERROR_MESSAGE.INVALID_MA_DICH_VU);
			getSmsResult.setContent("");
			return getSmsResult;
		}
		
		SmsMO smsMo = SmsMoDAO.dequeue(commandCode, maDichVu);
		// Step 2: Check a record in MO
		if ((smsMo.getRequestId() == null)){
			getSmsResult.setErrorCode(IConstant.ERROR_CODE.NO_RECORD);
			getSmsResult.setErrorMessage(IConstant.ERROR_MESSAGE.NO_RECORD);
		}
		//Step 3: get the first record in MO
		else{
			getSmsResult.setErrorCode(IConstant.ERROR_CODE.SUCCESS);
			getSmsResult.setErrorMessage(IConstant.ERROR_MESSAGE.SUCCESS);
			getSmsResult.setContent(smsMo.getRequestId() + "|" + smsMo.getPhoneNumber() + "|" + smsMo.getMaDichVu() + "|" + smsMo.getCommandCode() + "|" + smsMo.getInfo());
		}
		//Step 4: return result
		return getSmsResult;
	}
	
	

	public static void main1(String args[]){
		//GetSmsResult getSmsResult = MainProcess.getSMS();
		//System.out.println(getSmsResult.getErrorMessage());
		//System.out.println(getSmsResult.getContent());
	}




	public static SendSmsResult sendSMS(int requestId, String phoneNumber, String commandCode, String maDichVu, String info) {
		SendSmsResult sendSmsResult = new SendSmsResult();
		String dauSo = getDauSoDichVu(maDichVu);
		//check services...
		if(!ServiceConfig.checkCommandCode(commandCode)){
			sendSmsResult.setErrorCode(IConstant.ERROR_CODE.INVALID_COMMAND_CODE);
			sendSmsResult.setErrorMessage(IConstant.ERROR_MESSAGE.INVALID_COMMAND_CODE);
			return sendSmsResult;
		}
	
		if(!ServiceConfig.checkMaDichVu(dauSo)){
			sendSmsResult.setErrorCode(IConstant.ERROR_CODE.INVALID_MA_DICH_VU);
			sendSmsResult.setErrorMessage(IConstant.ERROR_MESSAGE.INVALID_MA_DICH_VU);
			return sendSmsResult;
		}
		
		
		int result = SmsMtDAO.enqueue(requestId, phoneNumber, commandCode, maDichVu, info);
		if(result == IConstant.ENQUEUE.SUCCESS){
			sendSmsResult.setErrorCode(IConstant.ERROR_CODE.SUCCESS);
			sendSmsResult.setErrorMessage(IConstant.ERROR_MESSAGE.SEND_SMS_SUCCESS);
		}else{
			sendSmsResult.setErrorCode(IConstant.ERROR_CODE.FAIL);
			sendSmsResult.setErrorMessage(IConstant.ERROR_MESSAGE.SEND_SMS_FAIL);
		}
		return sendSmsResult;
	}



	public static String getDauSoDichVu(String maDichVu) {
		if((maDichVu == null) || (maDichVu.equals("")) || (maDichVu.length() != 4)){
			return "";
		}else{
			StringBuilder temp = new StringBuilder(maDichVu);
			temp.setCharAt(1, 'x');
			return temp.toString();
		}
	}
	
	public static void main(String args[]){
		//System.out.println(MainProcess.getDauSoDichVu("7045"));
		System.out.println(ServiceConfig.checkCommandCode("ht"));
		System.out.println(ServiceConfig.checkMaDichVu("7x4s5"));
	}
	
}
